/*
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.iisageek.client.widgets.sortabletable;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.iisageek.client.dialogs.LoadingDialog;

/**
 * 
 * @author Bob Breznak
 * 
 * All special-use ExtSortTables should extend this.
 * 
 * i.e. ContactTable
 * 
 */

public abstract class SpecializedTable implements TableListener {

	private LoadingDialog loading = new LoadingDialog();

	protected int rows;

	protected SortableTable table = new SortableTable();

	public void showLoading() {
		loading.show();
	}

	public void hideLoading() {
		loading.hide();
	}

	public abstract void add(Object element);

	/**
	 * tablePopup
	 * 
	 * Generates and displays a popup with additional data for the row
	 * 
	 * @param sender
	 * @param row
	 * @param cell
	 */
	public abstract void tablePopup(SourcesTableEvents sender, int row, int cell);

	/**
	 * init
	 * 
	 * Initialize the table and add headers
	 * 
	 */
	protected abstract void init();

	/**
	 * SpecializedTable
	 * 
	 * the SpecializedTable constructor
	 * 
	 */
	public SpecializedTable() {
		table.setWidth("100%");
		table.setStyleName("sortableTable");
		table.setBorderWidth(0);
		table.setCellPadding(5);
		table.setCellSpacing(3);

		init();

		table.addTableListener(this);

	}

	/**
	 * generateTable
	 * 
	 * Renders the table, adds formatting and loads the css
	 * 
	 * @return SortableTable
	 */
	public SortableTable generateTable() {
		RowFormatter rowFormatter = table.getRowFormatter();
		rowFormatter.setStyleName(0, "tableHeader");
		CellFormatter cellFormatter = table.getCellFormatter();

		for (int colIndex = 0; colIndex < table.getCellCount(0); colIndex++) {
			cellFormatter.setStyleName(0, colIndex, "headerStyle");
			cellFormatter.setAlignment(0, colIndex,
					HasHorizontalAlignment.ALIGN_CENTER,
					HasVerticalAlignment.ALIGN_MIDDLE);
		}
		for (int colIndex = 0; colIndex < table.getCellCount(0); colIndex++) {
			cellFormatter.setStyleName(0, colIndex, "headerStyle");
			cellFormatter.setAlignment(0, colIndex,
					HasHorizontalAlignment.ALIGN_CENTER,
					HasVerticalAlignment.ALIGN_MIDDLE);
		}
		return table;
	}

	public void clear() {
		table.clear();
	}
	
	public void formatTable() {
		RowFormatter rowFormatter = table.getRowFormatter();
		rowFormatter.setStyleName(0, "tableHeader");
		CellFormatter cellFormatter = table.getCellFormatter();
		
		int part = table.size() / 10;

		if (table.size() % 10 > 0) {
			part++;
		}
		
		Formater formater = new Formater(rowFormatter, cellFormatter);
		formater.scheduleRepeating(500);
		
	}

	private class Formater extends Timer {
		
		private RowFormatter rowFormatter;
		private CellFormatter cellFormatter;
		private int row=1;

		Formater(RowFormatter rf, CellFormatter cf) {
			this.rowFormatter = rf;
			this.cellFormatter = cf;
		}
		
		public void format() {
			if(row < table.size()) {
				for (int rowIndex = row; rowIndex < row+10; rowIndex++) {
					if (rowIndex % 2 == 0) {
						rowFormatter.setStyleName(rowIndex, "customRowStyle");
					} else {
						rowFormatter.setStyleName(rowIndex, "tableRow");
					}
					for (int colIndex = 0; colIndex < table.getCellCount(0); colIndex++) {
						cellFormatter.setStyleName(rowIndex, colIndex,
								"customFont");
						cellFormatter.setAlignment(rowIndex, colIndex,
								HasHorizontalAlignment.ALIGN_LEFT,
								HasVerticalAlignment.ALIGN_MIDDLE);
	
					}
				}
			} else {
				super.cancel();
			}
		}
		
		public void run() {
			format();
			row+=10;
		}
		
	}
	
	/**
	 * onCellClicked
	 * 
	 * Used to allow a SpecializedTable to become a tableListener
	 * 
	 * @param sender
	 *            (SourcesTableEvents)
	 * @param row
	 *            (int)
	 * @param cell
	 *            (int)
	 */
	public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
		tablePopup(sender, row, cell);
	}

}